package org.epes.laboratory.vcn.service.test;

import java.io.ByteArrayOutputStream;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.epes.laboratory.vcn.application.test.OptimizationApplication;
import org.epes.laboratory.vcn.utils.POICMISHelper;

public class RunOptimizationTask2 implements JavaDelegate {

	public void execute(DelegateExecution execution) throws Exception {
		
		// Get optimizationApplication process variable and update all values - INIT		
		
		OptimizationApplication oa = (OptimizationApplication) execution.getVariable("optimizationApplication");
		
		// x0y0
		String x0y0 = (String) execution.getVariable("x0y0");
		String x0 = x0y0.split(",")[0];
		String y0 = x0y0.split(",")[1];
		
		oa.setX0(Integer.parseInt(x0));
		oa.setY0(Integer.parseInt(y0));
		
		// x1y1
		String x1y1 = (String) execution.getVariable("x1y1");
		String x1 = x1y1.split(",")[0];
		String y1 = x1y1.split(",")[1];
		
		oa.setX1(Integer.parseInt(x1));
		oa.setY1(Integer.parseInt(y1));
		
		// x2y2
		String x2y2 = (String) execution.getVariable("x2y2");
		String x2 = x2y2.split(",")[0];
		String y2 = x2y2.split(",")[1];
		
		oa.setX2(Integer.parseInt(x2));
		oa.setY2(Integer.parseInt(y2));
		
		// x3y3
		String x3y3 = (String) execution.getVariable("x3y3");
		String x3 = x3y3.split(",")[0];
		String y3 = x3y3.split(",")[1];
		
		oa.setX3(Integer.parseInt(x3));
		oa.setY3(Integer.parseInt(y3));
		
		// x4y4
		String x4y4 = (String) execution.getVariable("x4y4");
		String x4 = x4y4.split(",")[0];
		String y4 = x4y4.split(",")[1];
		
		oa.setX4(Integer.parseInt(x4));
		oa.setY4(Integer.parseInt(y4));		
		
		// Get optimizationApplication process variable and update all values - END
		
		// Get the excel template and fill in with OptimizationApplication variables - INIT		
		
		// Get the excel template EPES_Lab_sim_INPUT-OUTPUT_02.xlsx from the VCN using CMIS		
		POICMISHelper helper = new POICMISHelper();		
		helper.openWorkbook("workspace://SpacesStore/035e9161-8668-409f-b68c-ee094c04973c");
		
		// Fill in the values in the template
		// Set x0, y0 values		
		helper.setCellValue(oa.getX0(), 10, 2, false);
		helper.setCellValue(oa.getY0(), 10, 3, false);
		
		// Set x1, y1 values
		helper.setCellValue(oa.getX1(), 11, 2, false);
		helper.setCellValue(oa.getY1(), 11, 3, false);
		
		// Set x2, y2 values
		helper.setCellValue(oa.getX2(), 12, 2, false);
		helper.setCellValue(oa.getY2(), 12, 3, false);
		
		// Set x3, y3 values
		helper.setCellValue(oa.getX3(), 13, 2, false);
		helper.setCellValue(oa.getY3(), 13, 3, false);
		
		// Set x4, y4 values
		helper.setCellValue(oa.getX4(), 14, 2, false);
		helper.setCellValue(oa.getY4(), 14, 3, false);
				
		// Set co2ManagedKnowledge value
		helper.setCellValue(oa.getCo2ManagedKnowledge(), 10, 6, false);
		
		// Evaluate all the cells with formulas
		helper.evaluateAll();		
		
		// Get the excel template and fill in with OptimizationApplication variables - END		
		
		// Send the excelSimulator to SOMO through REST or other Web Services technology - INIT
		
		// Write the document into the file	system			
		//helper.saveDocumentToFileSystem();
		
		// Get simulation input
		ByteArrayOutputStream excelSimulator = helper.getSimulationInput();
		
		// Test saving a document to the VCN
		//helper.saveDocumentToFolder(excelSimulator, "workspace://SpacesStore/22cefa0a-4e42-47a6-8013-4d2fd1fe12ac", 
		//		"ExcelSimulator", ".xslx", EXCEL_MIMETYPE);		
		// Send the excelSimulator to SOMO through REST or other Web Services technology - END
		
		// Get the solution from SOMO - INIT

		// Get the solution from SOMO - END
		
		// Set the solution as an OptimizationApplication field and update 
		// optimizationApplication process variable - INIT
		
		byte[] excelSolution = excelSimulator.toByteArray();
		oa.setExcelSolution(excelSolution);
		
		// Update optimizationApplication process variable
		execution.setVariable("optimizationApplication", oa);	
		
		// 2.5 Set the solution as an OptimizationApplication field and update 
		// optimizationApplication process variable - END
		
	}
}
